<?php
	/**
	 * 函数定义
	 */
	/*%**************************************************************************************************************%*/
	// Service 相关

	//获取bucket列表
	function get_service($obj){
		$response = $obj->list_bucket();
		_format($response);
	}

	/*%**************************************************************************************************************%*/
	// Bucket 相关

	//创建bucket
	function create_bucket($obj,$bucket){
		//$bucket = 'phpsdk'.time();
		$acl = ALIOSS::OSS_ACL_TYPE_PRIVATE;
		//$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
		//$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE;
		
		$response = $obj->create_bucket($bucket,$acl);
		_format($response);
	}


	//删除bucket
	function delete_bucket($obj,$bucket){
		//$bucket = 'phpsdk1349849369';
		
		$response = $obj->delete_bucket($bucket);
		_format($response);
	}

	//设置bucket ACL
	function set_bucket_acl($obj,$bucket){
		//$bucket = 'phpsdk1349849394';
		//$acl = ALIOSS::OSS_ACL_TYPE_PRIVATE;
		$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ;
		//$acl = ALIOSS::OSS_ACL_TYPE_PUBLIC_READ_WRITE;
		
		$response = $obj->set_bucket_acl($bucket,$acl);
		_format($response);
	}

	//获取bucket ACL
	function get_bucket_acl($obj,$bucket){
		//$bucket = 'phpsdk1349849394';
		$options = array(
			ALIOSS::OSS_CONTENT_TYPE => 'text/xml',
		);
			
		$response = $obj->get_bucket_acl($bucket,$options);
		_format($response);	
	}

	/*%**************************************************************************************************************%*/
	// Object 相关

	//获取object列表
	function list_object($obj,$bucket){
		//$bucket = 'phpsdk1349849394';
		$options = array(
			'delimiter' => '',
			'prefix' => '',
			'max-keys' => 100,
			//'marker' => 'myobject-1330850469.pdf',
		);
		
		$response = $obj->list_object($bucket,$options);	
		_format($response);
	}

	//创建目录
	function create_directory($obj){
		$bucket = 'phpsdk1349849394';
		$dir = 'myfoloder-'.time();
		
		$response  = $obj->create_object_dir($bucket,$dir);
		_format($response);
	}

	//通过内容上传文件
	function upload_by_content($obj,$content,$bucket,$object){
//		$bucket = 'th010';
//		$folder = 'productlogo/';
//		$object = $folder.'20004.jpg';
		$upload_file_options = array(
			'content' => $content,
			'length' => strlen($content),
//			ALIOSS::OSS_HEADERS => array(
//				'Expires' => '2016-10-01 08:00:00',
//			),
		);
		$response = $obj->upload_file_by_content($bucket,$object,$upload_file_options);
		if( $response->isOk()  && $response->status==200){
			$theheader=$response->header;
			$read_url=str_replace($bucket.'.'.$obj::DEFAULT_OSS_HOST,$obj::READ_DOMAIN,$theheader['_info']['url']);
			return array('obj_url'=>$read_url);
		}
		else{
			return false;
		}
		
//		for($index = 100;$index < 999;$index++){	
//			
//			//$object = $folder.'myobj_'.$index.'.txt';
//			$object = $folder.'myobj_'.$index.'.txt';
//			
//			$content  = 'uploadfile';
//			/**
//			for($i = 1;$i<100;$i++){
//				$content .= $i;
//			}
//			*/
//			
//			$upload_file_options = array(
//				'content' => $content,
//				'length' => strlen($content),
//				ALIOSS::OSS_HEADERS => array(
//					'Expires' => '2012-10-01 08:00:00',
//				),
//			);
//			
//			$response = $obj->upload_file_by_content($bucket,$object,$upload_file_options);
//			echo 'upload file {'.$object.'}'.($response->isOk()?'ok':'fail')."\n";
//		}
		//_format($response);
	}

	//通过路径上传文件
	function upload_by_file($obj,$bucket,$object,$file_path){
		//$bucket = 'phpsdk1349849394';
		//$object = 'demo/test_obj.exe';	
		//$file_path = "C:\\Documents and Settings\\Administrator\\My Documents\\Downloads\\mystery-case-files-madame-fate_s1_l1_gF2213T1L1_d1402696706.exe";
		
		$response = $obj->upload_file_by_file($bucket,$object,$file_path);
		if($response->status==200){
			$theheader=$response->header;
			$read_url=str_replace($bucket.'.'.$obj::DEFAULT_OSS_HOST,$obj::READ_DOMAIN,$theheader['_info']['url']);
			return array('obj_url'=>$read_url);
		}else{
			return false;
		}
	}

	//拷贝object
	function copy_object($obj){
			//copy object
			$from_bucket = 'phpsdk1349849394';
			$from_object = 'netbeans-7.1.2-ml-cpp-linux.sh';
			$to_bucket = 'phpsdk1349849394';
			$to_object = 'copy-netbeans-7.1.2-ml-cpp-linux-'.time().'.sh';

			$response = $obj->copy_object($from_bucket,$from_object,$to_bucket,$to_object);
			_format($response);
	}

	//获取object meta
	function get_object_meta($obj){
		$bucket = 'phpsdk1349849394';
		$object = 'netbeans-7.1.2-ml-cpp-linux.sh'; 

		$response = $obj->get_object_meta($bucket,$object);
		_format($response);
	}

	//删除object
	function delete_object($obj){
		$bucket = 'th010';	
		$object = 'object001/';
		$response = $obj->delete_object($bucket,$object);
		_format($response);
	}

	//删除objects
	function delete_objects($obj){
		$bucket = 'phpsdk1349849394';
		$objects = array('myfoloder-1349850940/','myfoloder-1349850941/',);   
		
		$options = array(
			'quiet' => false,
			//ALIOSS::OSS_CONTENT_TYPE => 'text/xml',
		);
		
		$response = $obj->delete_objects($bucket,$objects,$options);
		_format($response);
	}

	//获取object
	function get_object($obj,$bucket){
		//$bucket = 'phpsdk1349849394';
		$object = 'demo/test_obj.exe'; 
		
		$options = array(
			ALIOSS::OSS_FILE_DOWNLOAD => "d:\\11.exe",
			//ALIOSS::OSS_CONTENT_TYPE => 'txt/html',
		);	
		
		$response = $obj->get_object($bucket,$object,$options);
		_format($response);
	}

	//检测object是否存在
	function is_object_exist($obj){
		$bucket = 'phpsdk1349849394';
		$object = 'netbeans-7.1.2-ml-cpp-linux.sh';  
								
		$response = $obj->is_object_exist($bucket,$object);
		_format($response);
	}

	//通过multipart上传文件
	function upload_by_multi_part($obj){
		$bucket = 'phpsdk1349849394';
		$object = 'Mining.the.Social.Web-'.time().'.pdf';  //英文
		$filepath = "D:\\Book\\Mining.the.Social.Web.pdf";  //英文
			
		$options = array(
			ALIOSS::OSS_FILE_UPLOAD => $filepath,
			'partSize' => 5242880,
		);

		$response = $obj->create_mpu_object($bucket, $object,$options);
		_format($response);
	}

	//通过multipart上传整个目录
	function upload_by_dir($obj){
		$bucket = 'phpsdk1349849394';
		$dir = "D:\\alidata\\www\\logs\\aliyun.com\\oss\\";
		$recursive = false;
		
		$response = $obj->create_mtu_object_by_dir($bucket,$dir,$recursive);	
	}

	//通过multi-part上传整个目录(新版)
	function batch_upload_file($obj){
		$options = array(
			'bucket' 	=> 'phpsdk1349849394',
			'object'	=> 'picture',
			'directory' => 'D:\alidata\www\logs\aliyun.com\oss',
		);
		$response = $obj->batch_upload_file($options);
	}



	/*%**************************************************************************************************************%*/
	// 签名url 相关

	//生成签名url,主要用户私有权限下的访问控制
	function get_sign_url($obj){
		$bucket = 'phpsdk1349849394';
		$object = 'netbeans-7.1.2-ml-cpp-linux.sh';
		$timeout = 3600;

		$response = $obj->get_sign_url($bucket,$object,$timeout);
		var_dump($response);
	}

	/*%**************************************************************************************************************%*/
	// 结果 相关

    function _clip_object_url($object){
        $replace_str = array('./','../','//');
        foreach ($replace_str as $v) {
            $object = str_replace($v,'/',$object);
        }
        if (substr($object,0,1)==='/') $object = substr($object,1);
        if (substr($object,-1)!=='/') $object .= '/';
        return $object;
    }

	//格式化返回结果
	function _format($response) {
		echo '|-----------------------Start---------------------------------------------------------------------------------------------------'."\n";
		echo '|-Status:' . $response->status . "\n";
		echo '|-Body:' ."\n"; 
		echo $response->body . "\n";
		echo "|-Header:\n";
		print_r ( $response->header );
		echo '-----------------------End-----------------------------------------------------------------------------------------------------'."\n\n";
	}
?>